package chatserver.netio;


import chatserver.roles.GlobalRole;
import chatserver.roles.ChannelRole;
import chatserver.roles.Rights;
import chatserver.netio.ClientAttr;
import chatserver.objects.User;


public class Perms {

    public Perms() {
        this(0);
    }

    public Perms(int i) {
        bits = i;
    }

    public Perms(String s) {
        this();
        int i;
        int j;
        for (j = 0; j < s.length() && (i = s.indexOf(',', j)) >= 0; j = i + 1) {
            set(s.substring(j, i).trim());
        }

        set(s.substring(j).trim());
    }

    private void set(String s) {
        for (int i = 0; i < names.length; i++) {
            if (names[i].equalsIgnoreCase(s)) {
                bits |= 1 << (byte) i;
            }
        }

    }

    public int getBits() {
        return bits;
    }

    public Attr toAttr(byte byte0)
            throws Exception {
        return new ClientAttr(byte0, bits);
    }

    public static Perms fromAttr(Attr attr)
            throws Exception {
        return new Perms(attr.getInt());
    }

    public boolean get(byte byte0) {
        return (bits & 1 << byte0) != 0;
    }

    public void clear(byte byte0) {
        bits = bits & ~(1 << byte0);
    }

    public void clear(Perms perms) {
        bits = bits & ~perms.bits;
    }

    public void set(byte byte0) {
        bits = bits | 1 << byte0;
    }

    public void set(Perms perms) {
        bits = bits | perms.bits;
    }

    public void mask(Perms perms) {
        bits = bits & perms.bits;
    }

    public Perms mayUpdate(Perms perms) {
        int i;
        if ((bits & 0x20) != 0) {
            i = bits;
        } else if ((bits & 0x10) != 0) {
            i = bits & 0xffffffef;
            if ((perms.bits & 0x20) != 0) {
                i &= ~perms.bits;
            }
        } else {
            i = 0;
        }
        return new Perms(i);
    }

    @Override
    public int hashCode() {
        return bits;
    }

    @Override
    public boolean equals(Object obj) {
        return obj != null && getClass() == obj.getClass() && bits == ((Perms) obj).bits;
    }

    @Override
    public String toString() {
        StringBuilder stringbuffer = new StringBuilder();
        for (byte byte0 = 0; byte0 < 16; byte0++) {
            if (get(byte0)) {
                if (stringbuffer.length() != 0) {
                    stringbuffer.append(", ");
                }
                stringbuffer.append(names[byte0]);
            }
        }

        return stringbuffer.toString();
    }
    public static final byte TALK = 1;
    public static final byte KICK = 2;
    public static final byte BAN = 3;
    public static final byte OPERATOR = 4;
    public static final byte OWNER = 5;
    public static final byte IP = 6;
    public static final byte ADDCHANNEL = 7;
    public static final byte KILLCHANNEL = 8;
    public static final byte COLOR = 9;
    public static final byte IMAGE = 10;
    public static final byte FONT = 11;
    public static final byte TALKTO = 12;
    public static final byte BYPASSQUEUE = 13;
    public static final byte VIP = 14;
    public static final byte TEXTCOLOR = 15;
    public static final byte HIDE = 16;    
    public static final byte PERM_COUNT = 17;
    static final String names[] = {
        "0", "TALK", "KICK", "BAN", "OPERATOR", "OWNER", "IP", "ADDCHANNEL", "KILLCHANNEL", "COLOR",
        "IMAGE", "FONT", "TALKTO", "BYPASSQUEUE", "VIP", "TEXTCOLOR", "HIDE"
    };
    private int bits;

    public static Perms getGlobalUserPerms(User user) {
        Perms ret = new Perms();
        GlobalRole role = user.getDecidingGlobalRole();

        if (role.hasRightWithAnyModifier(Rights.Global.AdministrateServer)) {
            ret.set(Perms.OWNER);
        }
        if (role.hasRightWithAnyModifier(Rights.Global.ViewIP)) {
            ret.set(Perms.IP);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.ServerKick)) {
            ret.set(Perms.KICK);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.ServerBan)) {
            ret.set(Perms.BAN);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.ServerOp)) {
            ret.set(Perms.OPERATOR);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.KillChannels)) {
            ret.set(Perms.KILLCHANNEL);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.AdministrateServer)) {
            ret.set(Perms.BYPASSQUEUE);
        }
        if (role.hasRightWithAnyModifier(Rights.Global.AdministrateServer)) {
            ret.set(Perms.VIP);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.Talk)) {
            ret.set(Perms.TALK);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.TalkTo)) {
            ret.set(Perms.TALKTO);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.UseColor)) {
            ret.set(Perms.COLOR);
            ret.set(Perms.IMAGE);
            ret.set(Perms.FONT);
            ret.set(Perms.TEXTCOLOR);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.AddChannel)) {
            ret.set(Perms.ADDCHANNEL);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.CanHide)) {
            ret.set(Perms.HIDE);
        }
        
        return ret;
    }

    public static Perms getChannelUserPerms(User user, ChannelRole channelRole) {
        Perms ret = new Perms();
        GlobalRole role = user.getDecidingGlobalRole();


        if (role.hasRightWithAnyModifier(Rights.Global.ViewIP)) {
            ret.set(Perms.IP);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.KillChannels)) {
            ret.set(Perms.KILLCHANNEL);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.TalkTo)) {
            ret.set(Perms.TALKTO);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.AddChannel)) {
            ret.set(Perms.ADDCHANNEL);
        }

        if (role.hasRightWithAnyModifier(Rights.Global.CanHide)) {
            ret.set(Perms.HIDE);
        }
        
        boolean isChannelAdmin = user.hasGlobalRight(Rights.Global.AdministrateChannels);
       
        if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.IsVIP)) {
            ret.set(Perms.BYPASSQUEUE);
        }
        if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.IsVIP)) {
            ret.set(Perms.VIP);
        }

        if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.Talk)) {
            ret.set(Perms.TALK);
        }    
 
         if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.TalkWithColor)) {
            ret.set(Perms.COLOR);
            ret.set(Perms.IMAGE);
            ret.set(Perms.FONT);
            ret.set(Perms.TEXTCOLOR);
        }       
        
        if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.IsCreator)) {
            ret.set(Perms.OWNER);
        }
        if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.Kick)) {
            ret.set(Perms.KICK);
        }

        if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.Ban)) {
            ret.set(Perms.BAN);
        }

        if (isChannelAdmin || channelRole.hasRightWithAnyModifier(Rights.Channel.Op)) {
            ret.set(Perms.OPERATOR);
        }      

        return ret;
    }
}
